# Adogs
# 时间: 2021/7/25 14:41
import pygame

from actor import WalkDir
from actor.RedArmy_walk_soldier import RedArmyWalkSoldier
from actor.japanese_walk_soldier import JapaneseWalkSoldier
from actor.treasure_sprite import TreasureSprite
from actor.xiao_tie import XiaoTie
from pytmx import load_pygame
from scene import BaseScene, ScenePassStatus
from scene.task import TaskStatus
from scene.task.task5_way_to_win import WayToWinTask


class TunnelWarScene2(BaseScene):
    """
    场景二
    """

    def __init__(self, xiao_tie):
        super(TunnelWarScene2, self).__init__(xiao_tie, "resources/tmx/map_2.jpg")
        self.tiled_map = load_pygame("resources/tmx/map_2_1.tmx")
        self.obstacle_group = pygame.sprite.Group()
        self.red_army_group = pygame.sprite.Group()
        self.japanese_group = pygame.sprite.Group()
        self.init_actor()
        self.init_task()
        self.view_x = 0
        self.view_y = 0

    def init_actor(self):
        for group in self.tiled_map.objectgroups:
            if group.name == '障碍物':
                for obj in group:
                    obstacle = pygame.sprite.Sprite()
                    obstacle.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                    self.obstacle_group.add(obstacle)
            elif group.name == '日本人':
                for obj in group:
                    japanese = JapaneseWalkSoldier(obj.x, obj.y)
                    self.japanese_group.add(japanese)
            elif group.name == '红军':
                for obj in group:
                    red_army = RedArmyWalkSoldier(obj.x, obj.y)
                    self.hong_jun = red_army
                    self.hong_jun.walk_action.set_dir(WalkDir.right)
            elif group.name == '小铁':
                for obj in group:
                    self.xiao_tie.set_pos(obj.x, obj.y)

    def init_task(self):
        self.tasks = []
        self.font = pygame.font.Font("resources/font/975宋体.ttf", 13)  # 加载字体

        self.tasks.append(WayToWinTask(self.xiao_tie, self.hong_jun, self.font))

        self.task_id = 0

    def draw_actor(self):
        self.xiao_tie.draw(self.current_surface, 0, 0)
        self.tasks[self.task_id].draw(self.current_surface, self.view_x, self.view_y)  # 绘制任务

    def run(self, down_flag, key_list):
        self.tasks[self.task_id].do_task(down_flag, key_list)
        self.xiao_tie.run(down_flag, key_list, self.obstacle_group, self.japanese_group, self.red_army_group)
        if down_flag and key_list[pygame.K_1]:
            self.pass_status = ScenePassStatus.final_win
        if down_flag and key_list[pygame.K_2]:
            self.pass_status = ScenePassStatus.final_fail

        task_status = self.tasks[self.task_id].get_status()
        if task_status == TaskStatus.win_over:
            self.task_id += 1
            if self.task_id >= len(self.tasks):
                self.pass_status = ScenePassStatus.final_win

        elif task_status == TaskStatus.fail_over:
            self.pass_status = ScenePassStatus.final_fail

    def get_pass_status(self):
        if self.pass_status == ScenePassStatus.final_win:
            return self.pass_status, "WinScene"
        elif self.pass_status == ScenePassStatus.final_fail:
            return self.pass_status, "FailScene"
        else:
            return self.pass_status, None
